﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MedicalSystems
{
    public partial class Form1 : Form
    {
        private int CurrRow;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Globals.dataSet = new List<Record>();
            loadData("DB.txt");
            Globals.setTxnFinish();
            grdMain.DataSource = Globals.dataSet;
            this.CurrRow = 0;
            //filterToolStripMenuItem_Click(sender, e);
        }


        private void loadData(string filename)
        {
            string[] lines = System.IO.File.ReadAllLines(filename);
            for(int i=1;i<lines.Length;++i)
            {
                string line = lines[i];
                string[] vals = line.Split('\t');
                if (vals[5] == "") vals[5] = "N/A";
                if (vals.Length > 9)
                {
                    Globals.dataSet.Add(new Record(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5], vals[6], vals[7], vals[8], vals[9]));
                }
                else
                {
                    Globals.dataSet.Add(new Record(vals[0], vals[1], vals[2], vals[3], vals[4], vals[5], vals[6], vals[7],vals[8], null));
                }
                
            }

            /*
            foreach (Record r in dataSet)
            {
                txtDebug.Text += r + "\n";
            }
             */
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        public void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter("DB.txt"))
            {
                file.WriteLine("First name\tLast name\tGender\tParameter name\tValue\tUnit\tValid start time\tValid stop time\tTransaction time\tTransaction end");
                foreach (Record line in Globals.dataSet)
                {
                    file.WriteLine(line);
                }
            }
        }

        private void grdMain_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            int row = e.RowIndex;
            int col = e.ColumnIndex;

            //cannot change patient name
            if (col > 0)
            {
                Globals.dataSet[row].setValueByIndex(grdMain.Rows[row].Cells[col].Value.ToString(), col);
            }
        }

        private void addToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            AddRecord ar = new AddRecord(this);
            ar.Show();
        }

        public void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Form1_Load(sender, e);
        }

        private void deleteRecordToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult res = MessageBox.Show("Are you sure you want to delete the following record?\n"
                                                + Globals.dataSet[CurrRow],
                                                "Delete Record", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
            if (res == System.Windows.Forms.DialogResult.OK)
            {
                Globals.dataSet.RemoveAt(this.CurrRow);
                grdMain.Refresh();
            }
        }

        private void grdMain_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex != -1)
            {
                this.CurrRow = e.RowIndex;
            }
        }

        private void grdMain_DataError(object sender, DataGridViewDataErrorEventArgs e)
        {

        }

        private void filterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Filter filter = new Filter();
            filter.Show();
        }

        private void advancedFilterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AdvancedFilter af = new AdvancedFilter();
            af.Show();
        }
    }
}
